<?php
session_start();
require_once 'config.php';

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    $stmt = $pdo->prepare("SELECT password FROM admin WHERE username = ? LIMIT 1");
    $stmt->execute([$username]);
    $row = $stmt->fetch();
    if ($row && password_verify($password, $row['password'])) {
        $_SESSION['admin_logged_in'] = true;
        $_SESSION['admin_username'] = $username;
        $_SESSION['admin_logged_in'] = true;
        $_SESSION['admin_login_time'] = time(); // 记录登录时间戳
        header('Location: announcement_manage.php');
        exit;
    } else {
        $error = '用户名或密码错误';
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理登录</title>
    <!-- 引入图标库以保持风格统一 -->
    <link rel="stylesheet" href="./resource/css/materialdesignicons.min(1).css">   <!-- 本地CSS路径 -->
    <style>
        /* 基础样式与管理界面统一 */
        body {
            font-family: "Segoe UI", "Microsoft YaHei", "Heiti SC", "WenQuanYi Micro Hei", sans-serif;
            background: #f5f7fa; /* 与管理界面背景色一致 */
            margin: 0;
            padding: 0;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        /* 登录容器采用卡片式设计，与管理界面的card类风格统一 */
        .login-container {
            width: 100%;
            max-width: 380px;
            background: #fff;
            border-radius: 16px; /* 与管理界面卡片圆角一致 */
            box-shadow: 0 4px 18px rgba(41, 128, 255, 0.08); /* 统一阴影效果 */
            padding: 40px 32px;
            display: flex;
            flex-direction: column;
            align-items: center;
            transition: box-shadow 0.2s; /* 统一hover动画 */
        }

        .login-container:hover {
            box-shadow: 0 8px 24px rgba(41, 128, 255, 0.12); /* 与管理界面卡片hover效果一致 */
        }

        /* 标题样式统一 */
        .login-title {
            font-size: 1.8em;
            font-weight: bold;
            margin-bottom: 32px;
            color: #2980ff; /* 主色调统一 */
            letter-spacing: 1.5px;
            display: flex;
            align-items: center;
        }

        .login-title .mdi {
            font-size: 1.5em;
            margin-right: 12px; /* 图标与文字间距 */
        }

        .login-form {
            width: 100%;
        }

        /* 输入框样式与管理界面表单控件统一 */
        .login-form input {
            width: 100%;
            font-size: 1em;
            padding: 12px 14px;
            margin: 14px 0;
            border-radius: 6px; /* 与管理界面输入框圆角一致 */
            border: 1px solid #ddd;
            box-sizing: border-box;
            transition: border-color 0.2s, box-shadow 0.2s; /* 统一聚焦动画 */
        }

        .login-form input:focus {
            outline: none;
            border-color: #2980ff; /* 聚焦时主色调边框 */
            box-shadow: 0 0 0 2px rgba(41, 128, 255, 0.2); /* 与管理界面输入框聚焦效果一致 */
        }

        /* 按钮样式与管理界面按钮统一 */
        .login-btn {
            width: 100%;
            padding: 10px 0;
            font-size: 1.1em;
            background: #2980ff; /* 主色调统一 */
            color: #fff;
            border: none;
            border-radius: 6px; /* 与管理界面按钮圆角一致 */
            cursor: pointer;
            transition: background 0.2s, box-shadow 0.2s; /* 统一过渡效果 */
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .login-btn .mdi {
            font-size: 1.2em;
        }

        .login-btn:hover {
            background: #1866d1; /* 与管理界面按钮hover色值一致 */
            box-shadow: 0 2px 8px rgba(41, 128, 255, 0.3); /* 统一hover阴影 */
        }

        /* 错误提示样式统一 */
        .error {
            color: #ff4d4f; /* 与管理界面错误色一致 */
            margin-bottom: 16px;
            width: 100%;
            text-align: center;
            font-size: 0.95em;
        }

        /* 响应式适配，与管理界面断点统一 */
        @media (max-width: 600px) {
            .login-container {
                max-width: 90vw;
                padding: 24px 16px;
            }

            .login-title {
                font-size: 1.5em;
                margin-bottom: 24px;
            }
        }
    </style>
</head>

<body>
    <div class="login-container">
        <div class="login-title">
            <span class="mdi mdi-lock"></span>后台登录
        </div>
        <?php if ($error): ?>
            <div class="error"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>
        <form class="login-form" method="post" autocomplete="off">
            <input type="text" name="username" placeholder="用户名" required autofocus>
            <input type="password" name="password" placeholder="密码" required>
            <button class="login-btn" type="submit">
                <span class="mdi mdi-login"></span>登录
            </button>
        </form>
    </div>
</body>

</html>